package vashion.azeroth.api.controller.tran;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.common.common.check.FormatCheck;
import vashion.azeroth.common.common.check.PayModeCheck;
import vashion.azeroth.common.net.IpUtil;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.TranErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.tran.pojo.PersonalRechargeOrder;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalMemberService;
import vashion.azeroth.core.service.tran.PersonalRechargeService;

import com.pingplusplus.model.Charge;


@Controller
@RequestMapping("/tran")
public class PersonalRechargeController {
	
	@Autowired
	private PersonalRechargeService<Charge> rechargeService;
	@Autowired
	private PersonalMemberService<PersonalMember> memberService;
	
	/**
	 * 获取充值凭证
	 * @param rechargeOrder
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping("obtainChargeCredential.htm")
	@ResponseBody
	public ApiResult<String> obtainChargeCredential(@ModelAttribute PersonalRechargeOrder rechargeOrder, String type, HttpServletRequest request) {
		
		if(!NumberUtils.isDigits(String.valueOf(rechargeOrder.getMemberId()))) {
			return ApiResult.newErrorResult(GenericErrorEnum.PARAM_FORMAT_ERROR.getErrorCode(), "memberId" + GenericErrorEnum.PARAM_FORMAT_ERROR.getErrorMessage());
		}
		
		PersonalMember member = memberService.get(rechargeOrder.getMemberId());
		if(null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		if(StringUtils.isBlank(rechargeOrder.getModeName())) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "支付方式" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		if(!PayModeCheck.checkPayMode(rechargeOrder.getModeName())) {
			return ApiResult.newErrorResult(TranErrorEnum.PAY_MODE_ERROR.getErrorCode(), TranErrorEnum.PAY_MODE_ERROR.getErrorMessage());
		}
		
		if(StringUtils.isBlank(String.valueOf(rechargeOrder.getAmount()))) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "付款金额" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		if(!FormatCheck.isLegalAmount(String.valueOf(rechargeOrder.getAmount()))) {
			return ApiResult.newErrorResult(GenericErrorEnum.PARAMETER_ERROR.getErrorCode(), "付款金额" + GenericErrorEnum.PARAMETER_ERROR.getErrorMessage());
		}
		
		ApiResult<String> result = null;
		String clientIp = IpUtil.getIpAddress(request);
		//充值
		if("pay".equals(type)) {
			result = rechargeService.obtainChargeCredential(rechargeOrder, member, clientIp);
		//消费
		} else if("disburse".equals(type)) {
			result = rechargeService.obtainDisburseCredential(rechargeOrder.getDisburseNo(), rechargeOrder.getModeName(), member, clientIp);
		//V币支付	
		} else if("vpay".equals(type)) {
			result = rechargeService.vpay(rechargeOrder.getDisburseNo(), clientIp);
		}
		
		return result;
	}
	
}
